/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
 *
 * This library is open source and may be redistributed and/or modified under  
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSGUTIL_LINESEGMENTINTERSECTOR_2
#define OSGUTIL_LINESEGMENTINTERSECTOR_2 1

#include <osgGIS/Common>
#include <osgUtil/IntersectionVisitor>

using namespace osg;
using namespace osgUtil;

namespace osgGIS
{
    /* (internal class - no public api docs)
     *
     * This is the LineSegmentIntersector class, patched to store valid nodePath's in the
     * Intersection inner class. The only change is the Intersection::nodePath member. Any
     * other changes are just from experimentation.
     */
    class LineSegmentIntersector2 : public Intersector
    {
    public:

        /** Construct a LineSegmentIntersector the runs between the secified start and end points in MODEL coordinates. */
        LineSegmentIntersector2(const osg::Vec3d& start, const osg::Vec3d& end);
        
        /** Construct a LineSegmentIntersector the runs between the secified start and end points in the specified coordinate frame. */
        LineSegmentIntersector2(CoordinateFrame cf, const osg::Vec3d& start, const osg::Vec3d& end);
        
        /** Convinience constructor for supporting picking in WINDOW, or PROJECTION coorindates
          * In WINDOW coordinates creates a start value of (x,y,0) and end value of (x,y,1).
          * In PROJECTION coordinates (clip space cube) creates a start value of (x,y,-1) and end value of (x,y,1).
          * In VIEW and MODEL coordinates creates a start value of (x,y,0) and end value of (x,y,1).*/
        LineSegmentIntersector2(CoordinateFrame cf, double x, double y);
        
        struct Intersection
        {
            Intersection():
                ratio(-1.0),
                primitiveIndex(0) {}
        
            bool operator < (const Intersection& rhs) const { return ratio < rhs.ratio; }

            typedef std::vector<unsigned int>   IndexList;
            typedef std::vector<double>         RatioList;

            double                          ratio;
            osg::NodeList                   nodePath;
            //osg::NodePath                   nodePath;
            osg::ref_ptr<osg::Drawable>     drawable;
            osg::ref_ptr<osg::RefMatrix>    matrix;
            osg::Vec3d                      localIntersectionPoint;
            osg::Vec3                       localIntersectionNormal;
            IndexList                       indexList;
            RatioList                       ratioList;
            unsigned int                    primitiveIndex;
            
            const osg::Vec3d& getLocalIntersectPoint() const { return localIntersectionPoint; }
            osg::Vec3d getWorldIntersectPoint() const { return matrix.valid() ? localIntersectionPoint * (*matrix) : localIntersectionPoint; }
            
            const osg::Vec3& getLocalIntersectNormal() const { return localIntersectionNormal; }
            osg::Vec3 getWorldIntersectNormal() const { return matrix.valid() ? osg::Matrix::transform3x3(osg::Matrix::inverse(*matrix),localIntersectionNormal) : localIntersectionNormal; }
        };
        
        typedef std::multiset<Intersection> Intersections;
        
        inline void insertIntersection(const Intersection& intersection) { getIntersections().insert(intersection); }

        inline Intersections& getIntersections() { return _parent ? _parent->_intersections : _intersections; }

        inline Intersection getFirstIntersection() { Intersections& intersections = getIntersections(); return intersections.empty() ? Intersection() : *(intersections.begin()); }
        
        inline void setStart(const osg::Vec3d& start) { _start = start; }
        inline const osg::Vec3d& getStart() const { return _start; }

        inline void setEnd(const osg::Vec3d& end) { _end = end; }
        inline const osg::Vec3d& setEnd() const { return _end; }

    public:

        virtual Intersector* clone(osgUtil::IntersectionVisitor& iv);
        
        virtual bool enter(const osg::Node& node);
        
        virtual void leave();
        
        virtual void intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable);
        
        virtual void reset();

        virtual bool containsIntersections() { return !_intersections.empty(); }

    protected:
    
        bool intersects(const osg::BoundingSphere& bs);
        bool intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const osg::BoundingBox& bb);

        LineSegmentIntersector2* _parent;

        osg::Vec3d  _start;
        osg::Vec3d  _end;
        
        Intersections _intersections;
    
};

}

#endif

